#!/bin/bash

cd /root/kube_etcd_src

# 将配置信息加载到session的环境变量中
source ./config/config.ini

# 获取主机名称
LOCAL_HOST_NAMES=$(hostname)
LOCAL_HOST_NAME=${LOCAL_HOST_NAMES%%.*}

# 获取主机IP
LOCAL_HOST_IP=$1

echo "LOCAL_HOST_NAME=" $LOCAL_HOST_NAME "LOCAL_HOST_IP=" $LOCAL_HOST_IP
# 准备etcd集群配置文件
cd /root/kube_etcd_src/config
\cp /root/kube_etcd_src/config/etcd-cluster.template /root/kube_etcd_src/config/etcd-cluster
sed -i "s/\$LOCAL_HOST_NAME/${LOCAL_HOST_NAME}/g" etcd-cluster
sed -i "s/\$LOCAL_HOST_IP/${LOCAL_HOST_IP}/g" etcd-cluster
sed -i "s/\$ETCD_NAME_1/${ETCD_NAME_1}/g" etcd-cluster
sed -i "s/\$ETCD_IP_1/${ETCD_IP_1}/g" etcd-cluster
sed -i "s/\$ETCD_NAME_2/${ETCD_NAME_2}/g" etcd-cluster
sed -i "s/\$ETCD_IP_2/${ETCD_IP_2}/g" etcd-cluster
sed -i "s/\$ETCD_NAME_3/${ETCD_NAME_3}/g" etcd-cluster
sed -i "s/\$ETCD_IP_3/${ETCD_IP_3}/g" etcd-cluster

# 准备etcd证书文件
mkdir -p /etc/etcd/ssl
cd /root/kube_etcd_src/certs
\cp /root/kube_etcd_src/certs/{etcd-ca-key.pem,etcd-ca.pem,etcd-key.pem,etcd.pem} /etc/etcd/ssl/

# 准备启动服务文件
rm -rf /data/etcd
mkdir -p /data/etcd
cd /root/kube_etcd_src/systemd
\cp /root/kube_etcd_src/systemd/etcd.service /lib/systemd/system/
\cp /root/kube_etcd_src/config/etcd-cluster /etc/etcd/etcd

# 二进制文件拷贝
cd /root/kube_etcd_src/bin
chmod +x /root/kube_etcd_src/bin/*
\cp /root/kube_etcd_src/bin/* /usr/local/bin

# 启动etcd服务
systemctl daemon-reload && systemctl stop etcd.service && systemctl enable etcd.service && systemctl start etcd.service
